from django.shortcuts import render, HttpResponse, redirect
import random
from utils.tencent.sms import send_sms_single
from app01 import models
from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError


# Create your views here.
def send_sms(request, *args, **kwargs):
    code = random.randint(1000, 9999)
    res = send_sms_single('15188397061', )


class RegisiterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(1[3-9])\d{9}$', '手机号格式错误')])
    password = forms.CharField(label='密码', widget=forms.PasswordInput())
    confirm_password = forms.CharField(label='重复密码', widget=forms.PasswordInput(attrs={'placeholder': '请重复输入密码'}))
    code = forms.CharField(label='验证码', widget=forms.TextInput(attrs={'placeholder': '请输入验证码'}))

    class Meta:
        model = models.UserInfo
        fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control %s'%(field.label,)
            field.widget.attrs['placeholder'] = '请输入%s' % (field.label,)


def register(request):
    form = RegisiterModelForm()
    return render(request, 'app01/register.html', {'form': form})

def ajax_send_msg(request):
    pass